package com.leetcode.Stack;

import java.util.Stack;

/**
 *  今天困了。。。没做出来
 */

public class Leetcode155 {

    Stack<Integer> stack;
    Stack<Integer> minStack; //栈顶保存当前最小值


    /** initialize your data structure here. */
    public Leetcode155() {
        stack = new Stack<>();
        minStack = new Stack<>(); //栈顶保存当前最小值
    }

    public void push(int x) {
        if (minStack.isEmpty() || x <= minStack.peek()) {
            minStack.push(x);
        }
        stack.push(x);
    }

    public void pop() {
        int currentValue = stack.peek();
        int currentMin = minStack.peek();
        if (currentValue == currentMin) {
            minStack.pop();
        }
        stack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        if (!minStack.isEmpty()) {
            return minStack.peek();
        }
        throw new NullPointerException("min stack is empty");
    }

    public static void main(String[] args) {
        Leetcode155 minStack = new Leetcode155();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-1);
        System.out.println(minStack.getMin());
        minStack.top();
        minStack.pop();
        System.out.println(minStack.getMin());
    }

}
